import time
from socket import *

def dictToTags(dictionary):
    output = str()
    for key in dictionary:
        output += "<"+str(key)+">"+str(dictionary[key])+"</"+str(key)+">"
    return output

def tagsToDict(tags):
    #
    output = dict()
    
    while len(tags) > 0:
        key = tags[1:][:tags.find(">")-1]
        # DATA CORRUPTION TEST
        if (tags.find(">") == -1):
            return  "DATAERROR"
        #print "KEY = "+key
        tmp = tags[tags.find(">")+1:]
        #print "TMP = "+tmp
            
        if (tmp.find("</"+key+">") == -1):
           return "DATAERROR"
        
        #print "TMP= "+ tmp
        #print "TMP FIND = "+str(tmp.find("</"+key+">"))
        value = tmp[:tmp.find("</"+key+">")]
        #print "VALUE = "+value
        #print "VALUE= "+ value
        
        try:
            # FOR USAGE IN THE KEY RECOVERY
            output[key] = int(value)
        except:
            output[key] = value
        tags = tags[tags.find("</"+key+">")+len("</"+key+">"):]
    return output
def splitChar(text, char = ":"):
    First = text[:text.find(char)]
    Last = text[text.find(char)+1:]
    return((First, Last))

def debug(text):
    f = open("debug.txt", "a")
    f.write(text+"\n")
    f.close()
    debugOn = True
    if debugOn:
        print text
    
